<!DOCTYPE html>
<html lang="zh-CN">
  <head>
  <meta charset="UTF-8">
  <meta 
    name="viewport"
    content="width=device-width, initial-scale=1.0, minimum-scale=1.0">
  <meta 
    http-equiv="X-UA-Compatible" 
    content="ie=edge">
  <meta 
    name="theme-color" 
    content="#fff" 
    id="theme-color">
  <meta 
    name="description" 
    content="hexoDemo用于介绍如何使用hexo">
  <link 
    rel="icon" 
    href="/">
  <title>36-DOM简介和DOM操作</title>
  
    
      <meta 
        property="og:title" 
        content="36-DOM简介和DOM操作">
    
    
      <meta 
        property="og:url" 
        content="https://zwl5670.gitee.io/2004/03/01/04-JavaScript%E5%9F%BA%E7%A1%80/36-DOM%E7%AE%80%E4%BB%8B%E5%92%8CDOM%E6%93%8D%E4%BD%9C/index.html">
    
    
      <meta 
        property="og:img" 
        content="https://img.songhn.com/img/Y67gdd.png">
    
    
      <meta 
        property="og:img" 
        content="hexoDemo用于介绍如何使用hexo">
    
    
      <meta 
        property="og:type" 
        content="article">
      <meta 
        property="og:article:published_time" 
        content="2004-03-01">
      <meta 
        property="og:article:modified_time" 
        content="2023-03-14">
      <meta 
        property="og:article:author" 
        content="zwl5670">
      
        
          <meta 
            property="og:article:tag" 
            content="JS基础">
        
      
    
  
  
  <link rel="preload" href="//at.alicdn.com/t/font_1946621_i1kgafibvw.css" as="style" >
  <link rel="preload" href="//at.alicdn.com/t/font_1952792_89b4ac4k4up.css" as="style" >
  <link rel="preload" href="/css/main.css" as="style" >
  
  <link rel="modulepreload" href="//instant.page/5.1.0">
  
  <link rel="stylesheet" href="/css/main.css">
  
  <link rel="stylesheet" href="//at.alicdn.com/t/font_1946621_i1kgafibvw.css">
  
  <link rel="stylesheet" href="//at.alicdn.com/t/font_1952792_89b4ac4k4up.css">
  
  
  
    <link rel="stylesheet" href="/js/lib/lightbox/baguetteBox.min.css">
  
  <script>
    function loadScript(url, cb) {
      var script = document.createElement('script');
      script.src = url;
      if (cb) script.onload = cb;
      script.async = true;
      document.body.appendChild(script);
    }
    function loadCSS(href, data, attr) {
      var sheet = document.createElement('link');
      sheet.ref = 'stylesheet';
      sheet.href = href;
      sheet.dataset[data] = attr;
      document.head.appendChild(sheet);
    }
    function changeCSS(cssFile, data, attr) {
      var oldlink = document.querySelector(data);
      var newlink = document.createElement("link");
      newlink.setAttribute("rel", "stylesheet");
      newlink.setAttribute("href", cssFile);
      newlink.dataset.prism = attr;
      document.head.replaceChild(newlink, oldlink);
    }
  </script>
  
    
  
  <script>
    // control reverse button
    var reverseDarkList = {
      dark: 'light',
      light: 'dark'
    };
    var themeColor = {
      dark: '#1c1c1e',
      light: '#fff'
    }
    // get the data of css prefers-color-scheme
    var getCssMediaQuery = function() {
      return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
    };
    // reverse current darkmode setting function
    var reverseDarkModeSetting = function() {
      var setting = localStorage.getItem('user-color-scheme');
      if(reverseDarkList[setting]) {
        setting = reverseDarkList[setting];
      } else if(setting === null) {
        setting = reverseDarkList[getCssMediaQuery()];
      } else {
        return;
      }
      localStorage.setItem('user-color-scheme', setting);
      return setting;
    };
    // apply current darkmode setting
  </script>
  
    <script>
      var setDarkmode = function(mode) {
      var setting = mode || localStorage.getItem('user-color-scheme');
      if(setting === getCssMediaQuery()) {
        document.documentElement.removeAttribute('data-user-color-scheme');
        localStorage.removeItem('user-color-scheme');
        document.getElementById('theme-color').content = themeColor[setting];
        document.getElementById('theme-color').dataset.mode = setting;
      } else if(reverseDarkList[setting]) {
        document.documentElement.setAttribute('data-user-color-scheme', setting);
        document.getElementById('theme-color').content = themeColor[setting];
        document.getElementById('theme-color').dataset.mode = setting;
      } else {
        document.documentElement.removeAttribute('data-user-color-scheme');
        localStorage.removeItem('user-color-scheme');
        document.getElementById('theme-color').content = themeColor[getCssMediaQuery()];
        document.getElementById('theme-color').dataset.mode = getCssMediaQuery();
      }
    };
    setDarkmode();
    </script>
  
  
  
    <link rel="preload" href="/js/lib/lightbox/baguetteBox.min.js" as="script">
    <link rel="preload" href="/js/lib/lightbox/baguetteBox.min.css" as="style" >
  
  
    <link rel="preload" href="/js/lib/lozad.min.js" as="script">
  
  
  
  
  
  
<meta name="generator" content="Hexo 6.3.0"></head>

  <body>
    <div class="wrapper">
       
      <nav class="navbar">
  <div class="navbar-logo">
    <a class="navbar-logo-main" href="/">
      
      <span class="navbar-logo-dsc">前端面试</span>
      </a>
  </div>
  <div class="navbar-menu">
    
      <a 
        href="/" 
        class="navbar-menu-item">
        
          首页
        
      </a>
    
      <a 
        href="/archives" 
        class="navbar-menu-item">
        
          归档
        
      </a>
    
      <a 
        href="/tags" 
        class="navbar-menu-item">
        
          标签
        
      </a>
    
      <a 
        href="/categories" 
        class="navbar-menu-item">
        
          分类
        
      </a>
    
      <a 
        href="/about" 
        class="navbar-menu-item">
        
          关于
        
      </a>
    
      <a 
        href="/links" 
        class="navbar-menu-item">
        
          友链
        
      </a>
    
    <button 
      class="navbar-menu-item darknavbar navbar-menu-btn" 
      aria-label="Toggle dark mode"
      id="dark">
      <i class="iconfont icon-weather"></i>
    </button>
    <button 
      class="navbar-menu-item searchnavbar navbar-menu-btn" 
      aria-label="Toggle search"
      id="search">
      <!-- <i 
        class="iconfont icon-search" 
        style="font-size: 1.2rem; font-weight: 400;">
      </i> -->
      <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img"
        class="iconify iconify--ion" width="28" height="28" preserveAspectRatio="xMidYMid meet" viewBox="0 0 512 512">
        <path fill="none" stroke="currentColor" stroke-miterlimit="10" stroke-width="28"
          d="M256 80a176 176 0 1 0 176 176A176 176 0 0 0 256 80Z"></path>
        <path fill="none" stroke="currentColor" stroke-miterlimit="10" stroke-width="28"
          d="M232 160a72 72 0 1 0 72 72a72 72 0 0 0-72-72Z"></path>
        <path fill="none" stroke="currentColor" stroke-linecap="round" stroke-miterlimit="10" stroke-width="28"
          d="M283.64 283.64L336 336"></path>
      </svg>
    </button>
  </div>
</nav> 
      
      <div 
        id="local-search" 
        style="display: none">
        <input
          class="navbar-menu-item"
          id="search-input"
          placeholder="请输入搜索内容..." />
        <div id="search-content"></div>
      </div>
      
      <div class="section-wrap">
        <div class="container">
          <div class="columns">
            <aside class="left-column">
              
              <div class="card card-author">
                
  <img 
    src="https://img.songhn.com/img/Y67gdd.png" 
    class="author-img"
    width="88"
    height="88"
    alt="author avatar">

<p class="author-name">zwl5670</p>
<p class="author-description">前端学习资料</p>
<div class="author-message">
  <a 
    class="author-posts-count" 
    href="/archives">
    <span>215</span>
    <span>文章</span>
  </a>
  <a 
    class="author-categories-count" 
    href="/categories">
    <span>14</span>
    <span>分类</span>
  </a>
  <a 
    class="author-tags-count" 
    href="/tags">
    <span>14</span>
    <span>标签</span>
  </a>
</div>

              </div>
               <div class="sticky-tablet">
  
  
    <article class="display-when-two-columns spacer">
      <div class="card card-content toc-card">
        <div class="toc-header">
  <i 
    class="iconfont icon-menu" 
    style="padding-right: 2px;">
  </i>目录
</div>
<ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%B8%B8%E8%A7%81%E6%A6%82%E5%BF%B5"><span class="toc-text">常见概念</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#JavaScript%E7%9A%84%E7%BB%84%E6%88%90"><span class="toc-text">JavaScript的组成</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%8A%82%E7%82%B9"><span class="toc-text">节点</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BB%80%E4%B9%88%E6%98%AFDOM"><span class="toc-text">什么是DOM</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#DOM%E5%8F%AF%E4%BB%A5%E5%81%9A%E4%BB%80%E4%B9%88"><span class="toc-text">DOM可以做什么</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%85%83%E7%B4%A0%E8%8A%82%E7%82%B9%E7%9A%84%E8%8E%B7%E5%8F%96"><span class="toc-text">元素节点的获取</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#DOM%E8%AE%BF%E9%97%AE%E5%85%B3%E7%B3%BB%E7%9A%84%E8%8E%B7%E5%8F%96"><span class="toc-text">DOM访问关系的获取</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%8E%B7%E5%8F%96%E7%88%B6%E8%8A%82%E7%82%B9"><span class="toc-text">获取父节点</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%8E%B7%E5%8F%96%E5%85%84%E5%BC%9F%E8%8A%82%E7%82%B9"><span class="toc-text">获取兄弟节点</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%8E%B7%E5%8F%96%E5%8D%95%E4%B8%AA%E7%9A%84%E5%AD%90%E8%8A%82%E7%82%B9"><span class="toc-text">获取单个的子节点</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%8E%B7%E5%8F%96%E6%89%80%E6%9C%89%E7%9A%84%E5%AD%90%E8%8A%82%E7%82%B9"><span class="toc-text">获取所有的子节点</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#DOM%E8%8A%82%E7%82%B9%E7%9A%84%E6%93%8D%E4%BD%9C%EF%BC%88%E9%87%8D%E8%A6%81%EF%BC%89"><span class="toc-text">DOM节点的操作（重要）</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%88%9B%E5%BB%BA%E8%8A%82%E7%82%B9"><span class="toc-text">创建节点</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%8F%92%E5%85%A5%E8%8A%82%E7%82%B9"><span class="toc-text">插入节点</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%88%A0%E9%99%A4%E8%8A%82%E7%82%B9"><span class="toc-text">删除节点</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%A4%8D%E5%88%B6%E8%8A%82%E7%82%B9%EF%BC%88%E5%85%8B%E9%9A%86%E8%8A%82%E7%82%B9%EF%BC%89"><span class="toc-text">复制节点（克隆节点）</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%AE%BE%E7%BD%AE%E8%8A%82%E7%82%B9%E7%9A%84%E5%B1%9E%E6%80%A7"><span class="toc-text">设置节点的属性</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1%E3%80%81%E8%8E%B7%E5%8F%96%E8%8A%82%E7%82%B9%E7%9A%84%E5%B1%9E%E6%80%A7%E5%80%BC"><span class="toc-text">1、获取节点的属性值</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2%E3%80%81%E8%AE%BE%E7%BD%AE%E8%8A%82%E7%82%B9%E7%9A%84%E5%B1%9E%E6%80%A7%E5%80%BC"><span class="toc-text">2、设置节点的属性值</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3%E3%80%81%E5%88%A0%E9%99%A4%E8%8A%82%E7%82%B9%E7%9A%84%E5%B1%9E%E6%80%A7"><span class="toc-text">3、删除节点的属性</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%80%BB%E7%BB%93"><span class="toc-text">总结</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#DOM%E5%AF%B9%E8%B1%A1%E7%9A%84%E5%B1%9E%E6%80%A7-%E8%A1%A5%E5%85%85"><span class="toc-text">DOM对象的属性-补充</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#innerHTML%E5%92%8CinnerText%E7%9A%84%E5%8C%BA%E5%88%AB"><span class="toc-text">innerHTML和innerText的区别</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#nodeType%E5%B1%9E%E6%80%A7"><span class="toc-text">nodeType属性</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#nodeType%E3%80%81nodeName%E3%80%81nodeValue"><span class="toc-text">nodeType、nodeName、nodeValue</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%96%87%E6%A1%A3%E7%9A%84%E5%8A%A0%E8%BD%BD"><span class="toc-text">文档的加载</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%88%91%E7%9A%84%E5%85%AC%E4%BC%97%E5%8F%B7"><span class="toc-text">我的公众号</span></a></li></ol>
      </div>
    </article>
  
  
  <article class="card card-content categories-widget">
    <div class="categories-card">
  <div class="categories-header">
    <i 
      class="iconfont icon-fenlei" 
      style="padding-right: 2px;">
    </i>分类
  </div>
  <div class="categories-list">
    
      <a href="/categories/%E5%89%8D%E7%AB%AF%E5%B7%A5%E5%85%B7/">
        <div class="categories-list-item">
          前端工具
          <span class="categories-list-item-badge">13</span>
        </div>
      </a>
    
      <a href="/categories/CSS%E5%9F%BA%E7%A1%80/">
        <div class="categories-list-item">
          CSS基础
          <span class="categories-list-item-badge">18</span>
        </div>
      </a>
    
      <a href="/categories/CSS%E8%BF%9B%E9%98%B6/">
        <div class="categories-list-item">
          CSS进阶
          <span class="categories-list-item-badge">9</span>
        </div>
      </a>
    
      <a href="/categories/HTML/">
        <div class="categories-list-item">
          HTML
          <span class="categories-list-item-badge">12</span>
        </div>
      </a>
    
      <a href="/categories/JS%E5%9F%BA%E7%A1%80/">
        <div class="categories-list-item">
          JS基础
          <span class="categories-list-item-badge">56</span>
        </div>
      </a>
    
      <a href="/categories/JS%E5%9F%BA%E7%A1%80ES6/">
        <div class="categories-list-item">
          JS基础ES6
          <span class="categories-list-item-badge">10</span>
        </div>
      </a>
    
      <a href="/categories/JS%E5%9F%BA%E7%A1%80%E5%BC%82%E6%AD%A5/">
        <div class="categories-list-item">
          JS基础异步
          <span class="categories-list-item-badge">11</span>
        </div>
      </a>
    
      <a href="/categories/JS%E8%BF%9B%E9%98%B6/">
        <div class="categories-list-item">
          JS进阶
          <span class="categories-list-item-badge">11</span>
        </div>
      </a>
    
      <a href="/categories/%E7%A7%BB%E5%8A%A8web/">
        <div class="categories-list-item">
          移动web
          <span class="categories-list-item-badge">3</span>
        </div>
      </a>
    
      <a href="/categories/CSS%E5%92%8CDOM/">
        <div class="categories-list-item">
          CSS和DOM
          <span class="categories-list-item-badge">6</span>
        </div>
      </a>
    
      <a href="/categories/Node/">
        <div class="categories-list-item">
          Node
          <span class="categories-list-item-badge">16</span>
        </div>
      </a>
    
      <a href="/categories/Vue%E5%9F%BA%E7%A1%80/">
        <div class="categories-list-item">
          Vue基础
          <span class="categories-list-item-badge">15</span>
        </div>
      </a>
    
      <a href="/categories/%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96/">
        <div class="categories-list-item">
          性能优化
          <span class="categories-list-item-badge">7</span>
        </div>
      </a>
    
      <a href="/categories/%E5%89%8D%E7%AB%AF%E5%B7%A5%E7%A8%8B%E5%8C%96/">
        <div class="categories-list-item">
          前端工程化
          <span class="categories-list-item-badge">8</span>
        </div>
      </a>
    
  </div>
</div>
  </article>
  
  <article class="card card-content tags-widget">
    <div class="tags-card">
  <div class="tags-header">
    <i 
      class="iconfont icon-biaoqian" 
      style="padding-right: 2px;">
    </i>热门标签
  </div>
  <div class="tags-list">
    
      <a 
        href="/tags/JS%E5%9F%BA%E7%A1%80/" 
        title="JS基础">
        <div class="tags-list-item">JS基础</div>
      </a>
    
      <a 
        href="/tags/CSS%E5%9F%BA%E7%A1%80/" 
        title="CSS基础">
        <div class="tags-list-item">CSS基础</div>
      </a>
    
      <a 
        href="/tags/Node/" 
        title="Node">
        <div class="tags-list-item">Node</div>
      </a>
    
      <a 
        href="/tags/Vue%E5%9F%BA%E7%A1%80/" 
        title="Vue基础">
        <div class="tags-list-item">Vue基础</div>
      </a>
    
      <a 
        href="/tags/%E5%89%8D%E7%AB%AF%E5%B7%A5%E5%85%B7/" 
        title="前端工具">
        <div class="tags-list-item">前端工具</div>
      </a>
    
      <a 
        href="/tags/HTML/" 
        title="HTML">
        <div class="tags-list-item">HTML</div>
      </a>
    
      <a 
        href="/tags/JS%E8%BF%9B%E9%98%B6/" 
        title="JS进阶">
        <div class="tags-list-item">JS进阶</div>
      </a>
    
      <a 
        href="/tags/JS%E5%9F%BA%E7%A1%80%E5%BC%82%E6%AD%A5/" 
        title="JS基础异步">
        <div class="tags-list-item">JS基础异步</div>
      </a>
    
      <a 
        href="/tags/JS%E5%9F%BA%E7%A1%80ES6/" 
        title="JS基础ES6">
        <div class="tags-list-item">JS基础ES6</div>
      </a>
    
      <a 
        href="/tags/CSS%E8%BF%9B%E9%98%B6/" 
        title="CSS进阶">
        <div class="tags-list-item">CSS进阶</div>
      </a>
    
      <a 
        href="/tags/%E5%89%8D%E7%AB%AF%E5%B7%A5%E7%A8%8B%E5%8C%96/" 
        title="前端工程化">
        <div class="tags-list-item">前端工程化</div>
      </a>
    
      <a 
        href="/tags/%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96/" 
        title="性能优化">
        <div class="tags-list-item">性能优化</div>
      </a>
    
      <a 
        href="/tags/CSS%E5%92%8CDOM/" 
        title="CSS和DOM">
        <div class="tags-list-item">CSS和DOM</div>
      </a>
    
      <a 
        href="/tags/%E7%A7%BB%E5%8A%A8web/" 
        title="移动web">
        <div class="tags-list-item">移动web</div>
      </a>
    
  </div>
</div>
  </article>
  
  
</div>
            </aside>
            <main class="main-column">
              
<article class="card card-content">
  <header>
    <h1 class="post-title">
      36-DOM简介和DOM操作
    </h1>
  </header>
  <div class="post-meta post-show-meta">
    <time datetime="2004-02-29T16:00:00.000Z">
      <i 
        class="iconfont icon-calendar" 
        style="margin-right: 2px;">
      </i>
      <span>2004-03-01</span>
    </time>
    
      <span class="dot"></span>
      
        <a 
          href="/categories/JS%E5%9F%BA%E7%A1%80/" 
          class="post-meta-link">
          JS基础
        </a>
      
    
    
      <span class="dot"></span>
      <span>4.3k 字</span>
    
  </div>
  
    <div 
      class="post-meta post-show-meta" 
      style="margin-top: -10px;">
      <div style="display: flex; align-items: center;">
        <i 
          class="iconfont icon-biaoqian" 
          style="margin-right: 2px; font-size: 1.15rem;">
        </i>
        
          
          <a 
            href="/tags/JS%E5%9F%BA%E7%A1%80/" 
            class="post-meta-link">
            JS基础
          </a>
        
      </div>
    </div>
  
  </header>
  <div 
    id="section" 
    class="post-content">
    <p><ArticleTopAd></ArticleTopAd></p>
<h2 id="常见概念"><a href="#常见概念" class="headerlink" title="常见概念"></a>常见概念</h2><h3 id="JavaScript的组成"><a href="#JavaScript的组成" class="headerlink" title="JavaScript的组成"></a>JavaScript的组成</h3><p>JavaScript基础分为三个部分：</p>
<ul>
<li><p>ECMAScript：JavaScript的语法标准。包括变量、表达式、运算符、函数、if语句、for语句等。</p>
</li>
<li><p><strong>DOM</strong>：文档对象模型（Document object Model），操作<strong>网页上的元素</strong>的API。比如让盒子移动、变色、轮播图等。</p>
</li>
<li><p><strong>BOM</strong>：浏览器对象模型（Browser Object Model），操作<strong>浏览器部分功能</strong>的API。比如让浏览器自动滚动。</p>
</li>
</ul>
<h3 id="节点"><a href="#节点" class="headerlink" title="节点"></a>节点</h3><p><strong>节点</strong>（Node）：构成 HTML 网页的最基本单元。网页中的每一个部分都可以称为是一个节点，比如：html标签、属性、文本、注释、整个文档等都是一个节点。</p>
<p>虽然都是节点，但是实际上他们的具体类型是不同的。常见节点分为四类：</p>
<ul>
<li><p>文档节点（文档）：整个 HTML 文档。整个 HTML 文档就是一个文档节点。</p>
</li>
<li><p>元素节点（标签）：HTML标签。</p>
</li>
<li><p>属性节点（属性）：元素的属性。</p>
</li>
<li><p>文本节点（文本）：HTML标签中的文本内容（包括标签之间的空格、换行）。</p>
</li>
</ul>
<p>节点的类型不同，属性和方法也都不尽相同。所有的节点都是Object。</p>
<h3 id="什么是DOM"><a href="#什么是DOM" class="headerlink" title="什么是DOM"></a>什么是DOM</h3><p><strong>DOM</strong>：Document Object Model，文档对象模型。DOM 为文档提供了结构化表示，并定义了如何通过脚本来访问文档结构。目的其实就是为了能让js操作html元素而制定的一个规范。</p>
<p>DOM就是由节点组成的。</p>
<p><strong>解析过程</strong>：<br>HTML加载完毕，渲染引擎会在内存中把HTML文档，生成一个DOM树，getElementById是获取内中DOM上的元素节点。然后操作的时候修改的是该元素的<strong>属性</strong>。</p>
<p><strong>DOM树</strong>：（一切都是节点）</p>
<p>DOM的数据结构如下：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20180126_2105.png" class="lozad post-image"src="http://img.smyhvae.com/20180126_2105.png"></p>
<p>上图可知，<strong>在HTML当中，一切都是节点</strong>（非常重要）。节点的分类，在上一段中，已经讲了。</p>
<p>整个html文档就是一个文档节点。所有的节点都是Object。</p>
<h3 id="DOM可以做什么"><a href="#DOM可以做什么" class="headerlink" title="DOM可以做什么"></a>DOM可以做什么</h3><ul>
<li><p>找对象（元素节点）</p>
</li>
<li><p>设置元素的属性值</p>
</li>
<li><p>设置元素的样式</p>
</li>
<li><p>动态创建和删除元素</p>
</li>
<li><p>事件的触发响应：事件源、事件、事件的驱动程序</p>
</li>
</ul>
<h2 id="元素节点的获取"><a href="#元素节点的获取" class="headerlink" title="元素节点的获取"></a>元素节点的获取</h2><p>DOM节点的获取方式其实就是<strong>获取事件源的方式</strong>。关于事件，上一篇文章中已经讲到了。</p>
<p>想要操作元素节点，必须首先要找到该节点。有三种方式可以获取DOM节点：</p>
<pre class="highlight"><span class="line"><span class="keyword">var</span> div1 = <span class="variable language_">document</span>.<span class="title function_">getElementById</span>(<span class="string">&quot;box1&quot;</span>); <span class="comment">//方式一：通过 id 获取 一个 元素节点（为什么是一个呢？因为 id 是唯一的）</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> arr1 = <span class="variable language_">document</span>.<span class="title function_">getElementsByTagName</span>(<span class="string">&quot;div&quot;</span>); <span class="comment">//方式二：通过 标签名 获取 元素节点数组，所以有s</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> arr2 = <span class="variable language_">document</span>.<span class="title function_">getElementsByClassName</span>(<span class="string">&quot;hehe&quot;</span>); <span class="comment">//方式三：通过 类名 获取 元素节点数组，所以有s</span></span><br></pre>

<p>既然方式二、方式三获取的是标签数组，那么习惯性是<strong>先遍历之后再使用</strong>。</p>
<p>特殊情况：数组中的值只有1个。即便如此，这一个值也是包在数组里的。这个值的获取方式如下：</p>
<pre class="highlight"><span class="line"><span class="variable language_">document</span>.<span class="title function_">getElementsByTagName</span>(<span class="string">&quot;div1&quot;</span>)[<span class="number">0</span>];    <span class="comment">//取数组中的第一个元素</span></span><br><span class="line"></span><br><span class="line"><span class="variable language_">document</span>.<span class="title function_">getElementsByClassName</span>(<span class="string">&quot;hehe&quot;</span>)[<span class="number">0</span>];  <span class="comment">//取数组中的第一个元素</span></span><br></pre>

<h2 id="DOM访问关系的获取"><a href="#DOM访问关系的获取" class="headerlink" title="DOM访问关系的获取"></a>DOM访问关系的获取</h2><p>DOM的节点并不是孤立的，因此可以通过DOM节点之间的相对关系对它们进行访问。如下：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20180126_2140.png" class="lozad post-image"src="http://img.smyhvae.com/20180126_2140.png"></p>
<p>节点的访问关系，是以<strong>属性</strong>的方式存在的。</p>
<p>JS中的<strong>父子兄</strong>访问关系：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20180126_2145.png" class="lozad post-image"src="http://img.smyhvae.com/20180126_2145.png"></p>
<p>这里我们要重点知道<strong>parentNode</strong>和<strong>children</strong>这两个属性的用法。下面分别介绍。</p>
<h3 id="获取父节点"><a href="#获取父节点" class="headerlink" title="获取父节点"></a>获取父节点</h3><p>调用者就是节点。一个节点只有一个父节点，调用方式就是</p>
<pre class="highlight"><span class="line">节点.<span class="property">parentNode</span></span><br></pre>

<h3 id="获取兄弟节点"><a href="#获取兄弟节点" class="headerlink" title="获取兄弟节点"></a>获取兄弟节点</h3><p><strong>1、下一个节点 | 下一个元素节点</strong>：</p>
<blockquote>
<p>Sibling的中文是<strong>兄弟</strong>。</p>
</blockquote>
<p>（1）nextSibling：</p>
<ul>
<li><p>火狐、谷歌、IE9+版本：都指的是下一个节点（包括标签、空文档和换行节点）。</p>
</li>
<li><p>IE678版本：指下一个元素节点（标签）。</p>
</li>
</ul>
<p>（2）nextElementSibling：</p>
<ul>
<li>火狐、谷歌、IE9+版本：都指的是下一个元素节点（标签）。</li>
</ul>
<p><strong>总结</strong>：为了获取下一个<strong>元素节点</strong>，我们可以这样做：在IE678中用nextSibling，在火狐谷歌IE9+以后用nextElementSibling，于是，综合这两个属性，可以这样写：</p>
<pre class="highlight"><span class="line">下一个兄弟节点 = 节点.<span class="property">nextElementSibling</span> || 节点.<span class="property">nextSibling</span></span><br></pre>

<p><strong>2、前一个节点 | 前一个元素节点</strong>：</p>
<blockquote>
<p>previous的中文是：前一个。</p>
</blockquote>
<p>（1）previousSibling：</p>
<ul>
<li><p>火狐、谷歌、IE9+版本：都指的是前一个节点（包括标签、空文档和换行节点）。</p>
</li>
<li><p>IE678版本：指前一个元素节点（标签）。</p>
</li>
</ul>
<p>（2）previousElementSibling：</p>
<ul>
<li>火狐、谷歌、IE9+版本：都指的是前一个元素节点（标签）。</li>
</ul>
<p><strong>总结</strong>：为了获取前一个<strong>元素节点</strong>，我们可以这样做：在IE678中用previousSibling，在火狐谷歌IE9+以后用previousElementSibling，于是，综合这两个属性，可以这样写：</p>
<pre class="highlight"><span class="line">前一个兄弟节点 = 节点.<span class="property">previousElementSibling</span> || 节点.<span class="property">previousSibling</span></span><br></pre>

<p><strong>3、补充</strong>：获得任意一个兄弟节点：</p>
<pre class="highlight"><span class="line">节点自己.<span class="property">parentNode</span>.<span class="property">children</span>[index];  <span class="comment">//随意得到兄弟节点</span></span><br></pre>

<h3 id="获取单个的子节点"><a href="#获取单个的子节点" class="headerlink" title="获取单个的子节点"></a>获取单个的子节点</h3><p><strong>1、第一个子节点 | 第一个子元素节点</strong>：</p>
<p>（1）firstChild：</p>
<ul>
<li><p>火狐、谷歌、IE9+版本：都指的是第一个子节点（包括标签、空文档和换行节点）。</p>
</li>
<li><p>IE678版本：指第一个子元素节点（标签）。</p>
</li>
</ul>
<p>（2）firstElementChild：</p>
<ul>
<li>火狐、谷歌、IE9+版本：都指的是第一个子元素节点（标签）。</li>
</ul>
<p><strong>总结</strong>：为了获取第一个<strong>子元素节点</strong>，我们可以这样做：在IE678中用firstChild，在火狐谷歌IE9+以后用firstElementChild，于是，综合这两个属性，可以这样写：</p>
<pre class="highlight"><span class="line">第一个子元素节点 = 节点.<span class="property">firstElementChild</span> || 节点.<span class="property">firstChild</span></span><br></pre>

<p><strong>2、最后一个子节点 | 最后一个子元素节点</strong>：</p>
<p>（1）lastChild：</p>
<ul>
<li><p>火狐、谷歌、IE9+版本：都指的是最后一个子节点（包括标签、空文档和换行节点）。</p>
</li>
<li><p>IE678版本：指最后一个子元素节点（标签）。</p>
</li>
</ul>
<p>（2）lastElementChild：</p>
<ul>
<li>火狐、谷歌、IE9+版本：都指的是最后一个子元素节点（标签）。</li>
</ul>
<p><strong>总结</strong>：为了获取最后一个<strong>子元素节点</strong>，我们可以这样做：在IE678中用lastChild，在火狐谷歌IE9+以后用lastElementChild，于是，综合这两个属性，可以这样写：</p>
<pre class="highlight"><span class="line">最后一个子元素节点 = 节点.<span class="property">lastElementChild</span> || 节点.<span class="property">lastChild</span></span><br></pre>

<h3 id="获取所有的子节点"><a href="#获取所有的子节点" class="headerlink" title="获取所有的子节点"></a>获取所有的子节点</h3><p>（1）<strong>childNodes</strong>：标准属性。返回的是指定元素的<strong>子节点</strong>的集合（包括元素节点、所有属性、文本节点）。是W3C的亲儿子。</p>
<ul>
<li>火狐 谷歌等高本版会把换行也看做是子节点。</li>
</ul>
<p>用法：</p>
<pre class="highlight"><span class="line">子节点数组 = 父节点.<span class="property">childNodes</span>;   <span class="comment">//获取所有节点。</span></span><br></pre>

<p>（2）<strong>children</strong>：非标准属性。返回的是指定元素的<strong>子元素节点</strong>的集合。【重要】</p>
<ul>
<li>它只返回HTML节点，甚至不返回文本节点。</li>
<li>在IE6&#x2F;7&#x2F;8中包含注释节点（在IE678中，注释节点不要写在里面）。</li>
</ul>
<p>虽然不是标准的DOM属性，但它和innerHTML方法一样，得到了几乎所有浏览器的支持。</p>
<p>用法：（<strong>用的最多</strong>）</p>
<pre class="highlight"><span class="line">子节点数组 = 父节点.<span class="property">children</span>;   <span class="comment">//获取所有节点。用的最多。</span></span><br></pre>

<h2 id="DOM节点的操作（重要）"><a href="#DOM节点的操作（重要）" class="headerlink" title="DOM节点的操作（重要）"></a>DOM节点的操作（重要）</h2><p>上一段的内容：节点的<strong>访问关系</strong>都是<strong>属性</strong>。</p>
<p>本段的内容：节点的<strong>操作</strong>都是<strong>函数</strong>（方法）。</p>
<h3 id="创建节点"><a href="#创建节点" class="headerlink" title="创建节点"></a>创建节点</h3><p>格式如下：</p>
<pre class="highlight"><span class="line">新的标签(元素节点) = <span class="variable language_">document</span>.<span class="title function_">createElement</span>(<span class="string">&quot;标签名&quot;</span>);</span><br></pre>

<p>比如，如果我们想创建一个li标签，或者是创建一个不存在的adbc标签，可以这样做：</p>
<pre class="highlight"><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">type</span>=<span class="string">&quot;text/javascript&quot;</span>&gt;</span><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">    <span class="keyword">var</span> a1 = <span class="variable language_">document</span>.<span class="title function_">createElement</span>(<span class="string">&quot;li&quot;</span>);   <span class="comment">//创建一个li标签</span></span></span><br><span class="line"><span class="language-javascript">    <span class="keyword">var</span> a2 = <span class="variable language_">document</span>.<span class="title function_">createElement</span>(<span class="string">&quot;adbc&quot;</span>);   <span class="comment">//创建一个不存在的标签</span></span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">    <span class="variable language_">console</span>.<span class="title function_">log</span>(a1);</span></span><br><span class="line"><span class="language-javascript">    <span class="variable language_">console</span>.<span class="title function_">log</span>(a2);</span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">    <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="keyword">typeof</span> a1);</span></span><br><span class="line"><span class="language-javascript">    <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="keyword">typeof</span> a2);</span></span><br><span class="line"><span class="language-javascript"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre>

<p>打印结果：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20180127_1135.png" class="lozad post-image"src="http://img.smyhvae.com/20180127_1135.png"></p>
<h3 id="插入节点"><a href="#插入节点" class="headerlink" title="插入节点"></a>插入节点</h3><p>插入节点有两种方式，它们的含义是不同的。</p>
<p>方式1：</p>
<pre class="highlight"><span class="line">父节点.<span class="title function_">appendChild</span>(新的子节点);</span><br></pre>

<p>解释：父节点的最后插入一个新的子节点。</p>
<p>方式2：</p>
<pre class="highlight"><span class="line">父节点.<span class="title function_">insertBefore</span>(新的子节点,作为参考的子节点)</span><br></pre>

<p>解释：</p>
<ul>
<li>在参考节点前插入一个新的节点。</li>
<li>如果参考节点为null，那么他将在父节点里面的最后插入一个子节点。</li>
</ul>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20180127_1257.png" class="lozad post-image"src="http://img.smyhvae.com/20180127_1257.png"></p>
<p>我们可以看到，li标签确实被插入到了box1标签的里面，和box2并列了。</p>
<p>方式2的举例：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20180127_1302.png" class="lozad post-image"src="http://img.smyhvae.com/20180127_1302.png"></p>
<p>我们可以看到，b1标签被插入到了box1标签的里面，和a1标签并列，在a1标签的前面。</p>
<p><strong>特别强调：</strong></p>
<p>关于方式1的appendChild方法，这里要强调一下。比如，现在有下面这样一个div结构：</p>
<pre class="highlight"><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;box11&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;box12&quot;</span>&gt;</span>生命壹号<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;box21&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;box22&quot;</span>&gt;</span>永不止步<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br></pre>


<p>上方结构中，子盒子box12是在父亲box11里的，子盒子box22是在父亲box21里面的。现在，如果我调用方法<code>box11.appendChild(box22)</code>，<strong>最后产生的结果是：box22会跑到box11中</strong>（也就是说，box22不在box21里面了）。这是一个很神奇的事情：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20180129_2125.png" class="lozad post-image"src="http://img.smyhvae.com/20180129_2125.png"></p>
<h3 id="删除节点"><a href="#删除节点" class="headerlink" title="删除节点"></a>删除节点</h3><p>格式如下：</p>
<pre class="highlight"><span class="line">父节点.<span class="title function_">removeChild</span>(子节点);</span><br></pre>

<p>解释：<strong>用父节点删除子节点</strong>。必须要指定是删除哪个子节点。</p>
<p>如果我想删除自己这个节点，可以这么做：</p>
<pre class="highlight"><span class="line">node1.<span class="property">parentNode</span>.<span class="title function_">removeChild</span>(node1);</span><br></pre>

<h3 id="复制节点（克隆节点）"><a href="#复制节点（克隆节点）" class="headerlink" title="复制节点（克隆节点）"></a>复制节点（克隆节点）</h3><p>格式如下：</p>
<pre class="highlight"><span class="line">要复制的节点.<span class="title function_">cloneNode</span>();       <span class="comment">//括号里不带参数和带参数false，效果是一样的。</span></span><br><span class="line"></span><br><span class="line">要复制的节点.<span class="title function_">cloneNode</span>(<span class="literal">true</span>);</span><br></pre>

<p>括号里带不带参数，效果是不同的。解释如下：</p>
<ul>
<li><p>不带参数&#x2F;带参数false：只复制节点本身，不复制子节点。</p>
</li>
<li><p>带参数true：既复制节点本身，也复制其所有的子节点。</p>
</li>
</ul>
<h2 id="设置节点的属性"><a href="#设置节点的属性" class="headerlink" title="设置节点的属性"></a>设置节点的属性</h2><p>我们可以获取节点的属性值、设置节点的属性值、删除节点的属性。</p>
<p>我们就统一拿下面这个标签来举例：</p>
<pre class="highlight"><span class="line"><span class="tag">&lt;<span class="name">img</span> <span class="attr">src</span>=<span class="string">&quot;images/1.jpg&quot;</span> <span class="attr">class</span>=<span class="string">&quot;image-box&quot;</span> <span class="attr">title</span>=<span class="string">&quot;美女图片&quot;</span> <span class="attr">alt</span>=<span class="string">&quot;地铁一瞥&quot;</span> <span class="attr">id</span>=<span class="string">&quot;a1&quot;</span>&gt;</span></span><br></pre>

<p>下面分别介绍。</p>
<h3 id="1、获取节点的属性值"><a href="#1、获取节点的属性值" class="headerlink" title="1、获取节点的属性值"></a>1、获取节点的属性值</h3><p><strong>方式1</strong>：</p>
<pre class="highlight"><span class="line">元素节点.属性名;</span><br><span class="line">元素节点[属性名];</span><br></pre>

<p>举例：（获取节点的属性值）</p>
<pre class="highlight"><span class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">img</span> <span class="attr">src</span>=<span class="string">&quot;images/1.jpg&quot;</span> <span class="attr">class</span>=<span class="string">&quot;image-box&quot;</span> <span class="attr">title</span>=<span class="string">&quot;美女图片&quot;</span> <span class="attr">alt</span>=<span class="string">&quot;地铁一瞥&quot;</span> <span class="attr">id</span>=<span class="string">&quot;a1&quot;</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">type</span>=<span class="string">&quot;text/javascript&quot;</span>&gt;</span><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">    <span class="keyword">var</span> myNode = <span class="variable language_">document</span>.<span class="title function_">getElementsByTagName</span>(<span class="string">&quot;img&quot;</span>)[<span class="number">0</span>];</span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">    <span class="variable language_">console</span>.<span class="title function_">log</span>(myNode.<span class="property">src</span>);</span></span><br><span class="line"><span class="language-javascript">    <span class="variable language_">console</span>.<span class="title function_">log</span>(myNode.<span class="property">className</span>);    <span class="comment">//注意，是className，不是class</span></span></span><br><span class="line"><span class="language-javascript">    <span class="variable language_">console</span>.<span class="title function_">log</span>(myNode.<span class="property">title</span>);</span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">    <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&quot;------------&quot;</span>);</span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">    <span class="variable language_">console</span>.<span class="title function_">log</span>(myNode[<span class="string">&quot;src&quot;</span>]);</span></span><br><span class="line"><span class="language-javascript">    <span class="variable language_">console</span>.<span class="title function_">log</span>(myNode[<span class="string">&quot;className&quot;</span>]); <span class="comment">//注意，是className，不是class</span></span></span><br><span class="line"><span class="language-javascript">    <span class="variable language_">console</span>.<span class="title function_">log</span>(myNode[<span class="string">&quot;title&quot;</span>]);</span></span><br><span class="line"><span class="language-javascript"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br></pre>

<p>上方代码中的img标签，有各种属性，我们可以逐一获取，打印结果如下：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20180127_1340.png" class="lozad post-image"src="http://img.smyhvae.com/20180127_1340.png"></p>
<p><strong>方式2</strong>：</p>
<pre class="highlight"><span class="line">元素节点.<span class="title function_">getAttribute</span>(<span class="string">&quot;属性名称&quot;</span>);</span><br></pre>

<p>举例：</p>
<pre class="highlight"><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(myNode.<span class="title function_">getAttribute</span>(<span class="string">&quot;src&quot;</span>));</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(myNode.<span class="title function_">getAttribute</span>(<span class="string">&quot;class&quot;</span>));   <span class="comment">//注意是class，不是className</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(myNode.<span class="title function_">getAttribute</span>(<span class="string">&quot;title&quot;</span>));</span><br></pre>

<p>打印结果：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20180127_1345.png" class="lozad post-image"src="http://img.smyhvae.com/20180127_1345.png"></p>
<p>方式1和方式2的区别在于：前者是直接操作标签，后者是把标签作为DOM节点。推荐方式2。</p>
<h3 id="2、设置节点的属性值"><a href="#2、设置节点的属性值" class="headerlink" title="2、设置节点的属性值"></a>2、设置节点的属性值</h3><p>方式1举例：（设置节点的属性值）</p>
<pre class="highlight"><span class="line">myNode.<span class="property">src</span> = <span class="string">&quot;images/2.jpg&quot;</span>   <span class="comment">//修改src的属性值</span></span><br><span class="line">myNode.<span class="property">className</span> = <span class="string">&quot;image2-box&quot;</span>;  <span class="comment">//修改class的name</span></span><br></pre>

<p>方式2：</p>
<pre class="highlight"><span class="line">元素节点.<span class="title function_">setAttribute</span>(<span class="string">&quot;属性名&quot;</span>, <span class="string">&quot;新的属性值&quot;</span>);</span><br></pre>

<p>方式2举例：（设置节点的属性值）</p>
<pre class="highlight"><span class="line">myNode.<span class="title function_">setAttribute</span>(<span class="string">&quot;src&quot;</span>,<span class="string">&quot;images/3.jpg&quot;</span>);</span><br><span class="line">myNode.<span class="title function_">setAttribute</span>(<span class="string">&quot;class&quot;</span>,<span class="string">&quot;image3-box&quot;</span>);</span><br><span class="line">myNode.<span class="title function_">setAttribute</span>(<span class="string">&quot;id&quot;</span>,<span class="string">&quot;你好&quot;</span>);</span><br></pre>


<h3 id="3、删除节点的属性"><a href="#3、删除节点的属性" class="headerlink" title="3、删除节点的属性"></a>3、删除节点的属性</h3><p>格式：</p>
<pre class="highlight"><span class="line">元素节点.<span class="title function_">removeAttribute</span>(属性名);</span><br></pre>

<p>举例：（删除节点的属性）</p>
<pre class="highlight"><span class="line">myNode.<span class="title function_">removeAttribute</span>(<span class="string">&quot;class&quot;</span>);</span><br><span class="line">myNode.<span class="title function_">removeAttribute</span>(<span class="string">&quot;id&quot;</span>);</span><br></pre>


<h3 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h3><p>获取节点的属性值和设置节点的属性值，都有两种方式。</p>
<p><strong>如果是节点的“原始属性”</strong>（比如 普通标签的<code>class/className</code>属性、普通标签的<code>style</code>属性、普通标签的 title属性、img 标签的<code>src</code>属性、超链接的<code>href</code>属性等），<strong>方式1和方式2是等价的</strong>，可以混用。怎么理解混用呢？比如说：用 <code>div.title = &#39;我是标题&#39;</code>设置属性，用 <code>div.getAttribute(&#39;title&#39;)</code>获取属性，就是混用。</p>
<p>但如果是节点的“非原始属性”，比如：</p>
<pre class="highlight"><span class="line">div.<span class="property">aaa</span> = <span class="string">&#x27;qianguyihao&#x27;</span>;</span><br><span class="line"></span><br><span class="line">div.<span class="title function_">setAttribute</span>(<span class="string">&#x27;bbb&#x27;</span>, <span class="string">&#x27;qianguyihao&#x27;</span>);</span><br><span class="line"></span><br></pre>

<p>上面的这个“非原始属性”，在使用这两种方式时，是有区别的。区别如下：</p>
<ul>
<li><p>方式1 的<code>元素节点.属性</code>和<code>元素节点[属性]</code>：绑定的属性值不会出现在标签上。</p>
</li>
<li><p>方式2 的<code>get/set/removeAttribut</code>：绑定的属性值会出现在标签上。</p>
</li>
<li><p><strong>这两种方式不能交换使用</strong>，get值和set值必须使用同一种方法。</p>
</li>
</ul>
<p>举例：</p>
<pre class="highlight"><span class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">&quot;box&quot;</span> <span class="attr">title</span>=<span class="string">&quot;主体&quot;</span> <span class="attr">class</span>=<span class="string">&quot;asdfasdfadsfd&quot;</span>&gt;</span>我爱你中国<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span>&gt;</span><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">    <span class="keyword">var</span> div = <span class="variable language_">document</span>.<span class="title function_">getElementById</span>(<span class="string">&quot;box&quot;</span>);</span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">    <span class="comment">//采用方式一进行set</span></span></span><br><span class="line"><span class="language-javascript">    div.<span class="property">aaaa</span> = <span class="string">&quot;1111&quot;</span>;</span></span><br><span class="line"><span class="language-javascript">    <span class="variable language_">console</span>.<span class="title function_">log</span>(div.<span class="property">aaaa</span>);    <span class="comment">//打印结果：1111。可以打印出来，但是不会出现在标签上</span></span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">    <span class="comment">//采用方式二进行set</span></span></span><br><span class="line"><span class="language-javascript">    div.<span class="title function_">setAttribute</span>(<span class="string">&quot;bbbb&quot;</span>,<span class="string">&quot;2222&quot;</span>);    <span class="comment">//bbbb作为新增的属性，会出现在标签上</span></span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">    <span class="variable language_">console</span>.<span class="title function_">log</span>(div.<span class="title function_">getAttribute</span>(<span class="string">&quot;aaaa&quot;</span>));   <span class="comment">//打印结果：null。因为方式一的set，无法采用方式二进行get。</span></span></span><br><span class="line"><span class="language-javascript">    <span class="variable language_">console</span>.<span class="title function_">log</span>(div.<span class="property">bbbb</span>);                   <span class="comment">//打印结果：undefined。因为方式二的set，无法采用方式一进行get。</span></span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br></pre>


<h2 id="DOM对象的属性-补充"><a href="#DOM对象的属性-补充" class="headerlink" title="DOM对象的属性-补充"></a>DOM对象的属性-补充</h2><h3 id="innerHTML和innerText的区别"><a href="#innerHTML和innerText的区别" class="headerlink" title="innerHTML和innerText的区别"></a>innerHTML和innerText的区别</h3><ul>
<li><p>value：标签的value属性。</p>
</li>
<li><p><strong>innerHTML</strong>：双闭合标签里面的内容（包含标签）。</p>
</li>
<li><p><strong>innerText</strong>：双闭合标签里面的内容（不包含标签）。（老版本的火狐用textContent）</p>
</li>
</ul>
<p><strong>获取内容举例：</strong></p>
<p>如果我们想获取innerHTML和innerText里的内容，看看会如何：（innerHTML会获取到标签本身，而innerText则不会）</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20180127_1652.png" class="lozad post-image"src="http://img.smyhvae.com/20180127_1652.png"></p>
<p><strong>修改内容举例：</strong>（innerHTML会修改标签本身，而innerText则不会）</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20180127_1657.png" class="lozad post-image"src="http://img.smyhvae.com/20180127_1657.png"></p>
<h3 id="nodeType属性"><a href="#nodeType属性" class="headerlink" title="nodeType属性"></a>nodeType属性</h3><p>这里讲一下nodeType属性。</p>
<ul>
<li><p><strong>nodeType &#x3D;&#x3D; 1  表示的是元素节点</strong>（标签） 。记住：在这里，元素就是标签。</p>
</li>
<li><p>nodeType &#x3D;&#x3D; 2  表示是属性节点。</p>
</li>
<li><p>nodeType &#x3D;&#x3D; 3  是文本节点。</p>
</li>
</ul>
<h3 id="nodeType、nodeName、nodeValue"><a href="#nodeType、nodeName、nodeValue" class="headerlink" title="nodeType、nodeName、nodeValue"></a>nodeType、nodeName、nodeValue</h3><p>我们那下面这个标签来举例：</p>
<pre class="highlight"><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">&quot;box&quot;</span> <span class="attr">value</span>=<span class="string">&quot;111&quot;</span>&gt;</span></span><br><span class="line">    生命壹号</span><br><span class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br></pre>

<p>上面这个标签就包含了三种节点：</p>
<ul>
<li><p>元素节点（标签）</p>
</li>
<li><p>属性节点</p>
</li>
<li><p>文本节点</p>
</li>
</ul>
<p>获取这三个节点的方式如下：</p>
<pre class="highlight"><span class="line"><span class="keyword">var</span> element = <span class="variable language_">document</span>.<span class="title function_">getElementById</span>(<span class="string">&quot;box1&quot;</span>);  <span class="comment">//获取元素节点（标签）</span></span><br><span class="line"><span class="keyword">var</span> attribute = element.<span class="title function_">getAttributeNode</span>(<span class="string">&quot;id&quot;</span>); <span class="comment">//获取box1的属性节点</span></span><br><span class="line"><span class="keyword">var</span> txt = element.<span class="property">firstChild</span>;                   <span class="comment">//获取box1的文本节点</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> value = element.<span class="title function_">getAttribute</span>(<span class="string">&quot;id&quot;</span>);         <span class="comment">//获取id的属性值</span></span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(element);</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&quot;--------------&quot;</span>);</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(attribute);</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&quot;--------------&quot;</span>);</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(txt);</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&quot;--------------&quot;</span>);</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(value);</span><br></pre>

<p>打印结果如下：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20180128_1935.png" class="lozad post-image"src="http://img.smyhvae.com/20180128_1935.png"></p>
<p>既然这三个都是节点，如果我想获取它们的nodeType、nodeName、nodeValue，代码如下：</p>
<pre class="highlight"><span class="line"><span class="keyword">var</span> element = <span class="variable language_">document</span>.<span class="title function_">getElementById</span>(<span class="string">&quot;box1&quot;</span>);  <span class="comment">//获取元素节点（标签）</span></span><br><span class="line"><span class="keyword">var</span> attribute = element.<span class="title function_">getAttributeNode</span>(<span class="string">&quot;id&quot;</span>); <span class="comment">//获取box1的属性节点</span></span><br><span class="line"><span class="keyword">var</span> txt = element.<span class="property">firstChild</span>;                   <span class="comment">//获取box1的文本节点</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//获取nodeType</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(element.<span class="property">nodeType</span>);       <span class="comment">//1</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(attribute.<span class="property">nodeType</span>);     <span class="comment">//2</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(txt.<span class="property">nodeType</span>);           <span class="comment">//3</span></span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&quot;--------------&quot;</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">//获取nodeName</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(element.<span class="property">nodeName</span>);       <span class="comment">//DIV</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(attribute.<span class="property">nodeName</span>);     <span class="comment">//id</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(txt.<span class="property">nodeName</span>);           <span class="comment">//#text</span></span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&quot;--------------&quot;</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">//获取nodeValue</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(element.<span class="property">nodeValue</span>);     <span class="comment">//null</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(attribute.<span class="property">nodeValue</span>);   <span class="comment">//box1</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(txt.<span class="property">nodeValue</span>);         <span class="comment">//生命壹号</span></span><br></pre>

<p>打印结果如下：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20180128_1939.png" class="lozad post-image"src="http://img.smyhvae.com/20180128_1939.png"></p>
<h2 id="文档的加载"><a href="#文档的加载" class="headerlink" title="文档的加载"></a>文档的加载</h2><p>浏览器在加载一个页面时，是按照自上向下的顺序加载的，读取到一行就运行一行。如果将script标签写到页面的上边，在代码执行时，页面还没有加载，页面没有加载DOM对象也没有加载，会导致无法获取到DOM对象。</p>
<p><strong>onload 事件</strong>：</p>
<p>onload 事件会在整个页面加载完成之后才触发。为 window 绑定一个onload事件，该事件对应的响应函数将会在页面加载完成之后执行，这样可以确保我们的代码执行时所有的DOM对象已经加载完毕了。</p>
<p>代码举例：</p>
<pre class="highlight"><span class="line"><span class="meta">&lt;!DOCTYPE <span class="keyword">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">html</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">head</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">&quot;UTF-8&quot;</span> /&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">title</span>&gt;</span><span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">script</span> <span class="attr">type</span>=<span class="string">&quot;text/javascript&quot;</span>&gt;</span><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">      <span class="comment">// 【方式一：先加载，后执行】这段 js 代码是写在 &lt;head&gt; 标签里的，所以建议放在 window.onload 里面。</span></span></span><br><span class="line"><span class="language-javascript">      <span class="variable language_">window</span>.<span class="property">onload</span> = <span class="keyword">function</span>(<span class="params"></span>) &#123;</span></span><br><span class="line"><span class="language-javascript">        <span class="comment">// 获取id为btn的按钮</span></span></span><br><span class="line"><span class="language-javascript">        <span class="keyword">var</span> btn = <span class="variable language_">document</span>.<span class="title function_">getElementById</span>(<span class="string">&quot;btn&quot;</span>);</span></span><br><span class="line"><span class="language-javascript">        <span class="comment">// 为按钮绑定点击事件</span></span></span><br><span class="line"><span class="language-javascript">        btn.<span class="property">onclick</span> = <span class="keyword">function</span>(<span class="params"></span>) &#123;</span></span><br><span class="line"><span class="language-javascript">          <span class="title function_">alert</span>(<span class="string">&quot;hello&quot;</span>);</span></span><br><span class="line"><span class="language-javascript">        &#125;;</span></span><br><span class="line"><span class="language-javascript">      &#125;;</span></span><br><span class="line"><span class="language-javascript">    </span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">button</span> <span class="attr">id</span>=<span class="string">&quot;btn&quot;</span>&gt;</span>点我一下<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="tag">&lt;<span class="name">script</span> <span class="attr">type</span>=<span class="string">&quot;text/javascript&quot;</span>&gt;</span><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">      <span class="comment">// 【方式二：后加载，后执行】这段 js 代码是写在 &lt;body&gt; 标签里的，代码的位置是处在页面的下方。这么做，也可以确保：在页面加载完毕后，再执行 js 代码。</span></span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">      <span class="comment">// 获取id为btn的按钮</span></span></span><br><span class="line"><span class="language-javascript">      <span class="keyword">var</span> btn = <span class="variable language_">document</span>.<span class="title function_">getElementById</span>(<span class="string">&quot;btn&quot;</span>);</span></span><br><span class="line"><span class="language-javascript">      <span class="comment">// 为按钮绑定点击事件</span></span></span><br><span class="line"><span class="language-javascript">      btn.<span class="property">onclick</span> = <span class="keyword">function</span>(<span class="params"></span>) &#123;</span></span><br><span class="line"><span class="language-javascript">        <span class="title function_">alert</span>(<span class="string">&quot;hello&quot;</span>);</span></span><br><span class="line"><span class="language-javascript">      &#125;;</span></span><br><span class="line"><span class="language-javascript">    </span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br><span class="line"></span><br><span class="line"></span><br></pre>

<p>上方代码中，方式一和方式二均可以确保：在页面加载完毕后，再执行 js 代码。</p>
<h2 id="我的公众号"><a href="#我的公众号" class="headerlink" title="我的公众号"></a>我的公众号</h2><p>想学习<strong>更多技能</strong>？不妨关注我的微信公众号：<strong>千古壹号</strong>（id：<code>qianguyihao</code>）。</p>
<p>扫一扫，你将发现另一个全新的世界，而这将是一场美丽的意外：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20190101.png" class="lozad post-image"src="http://img.smyhvae.com/20190101.png"></p>

  </div>
  <div>
    
      <div 
        class="post-note note-warning copyright" 
        style="margin-top: 42px">
        <p>
          <span style="font-weight: bold;">作者：</span><a 
            target="_blank" 
            rel="nofollow noopener noreferrer" 
            href="/about">
            zwl5670
          </a>
        </p>
        <p>
          <span style="font-weight: bold;">文章链接：</span><a 
            target="_blank" 
            rel="nofollow noopener noreferrer" 
            href="https://zwl5670.gitee.io/2004/03/01/04-JavaScript%E5%9F%BA%E7%A1%80/36-DOM%E7%AE%80%E4%BB%8B%E5%92%8CDOM%E6%93%8D%E4%BD%9C/">
            https://zwl5670.gitee.io/2004/03/01/04-JavaScript%E5%9F%BA%E7%A1%80/36-DOM%E7%AE%80%E4%BB%8B%E5%92%8CDOM%E6%93%8D%E4%BD%9C/
          </a>
        </p>
        <p><span style="font-weight: bold;">版权声明：</span>本博客所有文章除特别声明外，均采用<a target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh">CC BY-NC-SA 4.0 协议</a>。转载请注明出处！</p>
      </div>
    
  </div>
</article>
<div class="nav">
  
    <div class="nav-item-prev">
      <a 
        href="/2004/03/01/04-JavaScript%E5%9F%BA%E7%A1%80/34-%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F/" 
        class="nav-link">
        <i class="iconfont icon-left nav-prev-icon"></i>
        <div>
          <div class="nav-label">上一篇</div>
          
            <div class="nav-title">34-正则表达式 </div>
          
        </div>
      </a>
    </div>
  
  
    <div class="nav-item-next">
      <a 
        href="/2004/03/01/04-JavaScript%E5%9F%BA%E7%A1%80/37-%E9%80%9A%E8%BF%87style%E5%AF%B9%E8%B1%A1%E8%8E%B7%E5%8F%96%E5%92%8C%E8%AE%BE%E7%BD%AE%E8%A1%8C%E5%86%85%E6%A0%B7%E5%BC%8F/" 
        class="nav-link">
        <div>
          <div class="nav-label">下一篇</div>
          
            <div class="nav-title">37-通过style对象获取和设置行内样式 </div>
          
        </div>
        <i class="iconfont icon-right nav-next-icon"></i>
      </a>
    </div>
  
</div>

<div 
  class="card card-content toc-card" 
  id="mobiletoc">
  <div class="toc-header">
  <i 
    class="iconfont icon-menu" 
    style="padding-right: 2px;">
  </i>目录
</div>
<ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%B8%B8%E8%A7%81%E6%A6%82%E5%BF%B5"><span class="toc-text">常见概念</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#JavaScript%E7%9A%84%E7%BB%84%E6%88%90"><span class="toc-text">JavaScript的组成</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%8A%82%E7%82%B9"><span class="toc-text">节点</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BB%80%E4%B9%88%E6%98%AFDOM"><span class="toc-text">什么是DOM</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#DOM%E5%8F%AF%E4%BB%A5%E5%81%9A%E4%BB%80%E4%B9%88"><span class="toc-text">DOM可以做什么</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%85%83%E7%B4%A0%E8%8A%82%E7%82%B9%E7%9A%84%E8%8E%B7%E5%8F%96"><span class="toc-text">元素节点的获取</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#DOM%E8%AE%BF%E9%97%AE%E5%85%B3%E7%B3%BB%E7%9A%84%E8%8E%B7%E5%8F%96"><span class="toc-text">DOM访问关系的获取</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%8E%B7%E5%8F%96%E7%88%B6%E8%8A%82%E7%82%B9"><span class="toc-text">获取父节点</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%8E%B7%E5%8F%96%E5%85%84%E5%BC%9F%E8%8A%82%E7%82%B9"><span class="toc-text">获取兄弟节点</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%8E%B7%E5%8F%96%E5%8D%95%E4%B8%AA%E7%9A%84%E5%AD%90%E8%8A%82%E7%82%B9"><span class="toc-text">获取单个的子节点</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%8E%B7%E5%8F%96%E6%89%80%E6%9C%89%E7%9A%84%E5%AD%90%E8%8A%82%E7%82%B9"><span class="toc-text">获取所有的子节点</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#DOM%E8%8A%82%E7%82%B9%E7%9A%84%E6%93%8D%E4%BD%9C%EF%BC%88%E9%87%8D%E8%A6%81%EF%BC%89"><span class="toc-text">DOM节点的操作（重要）</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%88%9B%E5%BB%BA%E8%8A%82%E7%82%B9"><span class="toc-text">创建节点</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%8F%92%E5%85%A5%E8%8A%82%E7%82%B9"><span class="toc-text">插入节点</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%88%A0%E9%99%A4%E8%8A%82%E7%82%B9"><span class="toc-text">删除节点</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%A4%8D%E5%88%B6%E8%8A%82%E7%82%B9%EF%BC%88%E5%85%8B%E9%9A%86%E8%8A%82%E7%82%B9%EF%BC%89"><span class="toc-text">复制节点（克隆节点）</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%AE%BE%E7%BD%AE%E8%8A%82%E7%82%B9%E7%9A%84%E5%B1%9E%E6%80%A7"><span class="toc-text">设置节点的属性</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1%E3%80%81%E8%8E%B7%E5%8F%96%E8%8A%82%E7%82%B9%E7%9A%84%E5%B1%9E%E6%80%A7%E5%80%BC"><span class="toc-text">1、获取节点的属性值</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2%E3%80%81%E8%AE%BE%E7%BD%AE%E8%8A%82%E7%82%B9%E7%9A%84%E5%B1%9E%E6%80%A7%E5%80%BC"><span class="toc-text">2、设置节点的属性值</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3%E3%80%81%E5%88%A0%E9%99%A4%E8%8A%82%E7%82%B9%E7%9A%84%E5%B1%9E%E6%80%A7"><span class="toc-text">3、删除节点的属性</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%80%BB%E7%BB%93"><span class="toc-text">总结</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#DOM%E5%AF%B9%E8%B1%A1%E7%9A%84%E5%B1%9E%E6%80%A7-%E8%A1%A5%E5%85%85"><span class="toc-text">DOM对象的属性-补充</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#innerHTML%E5%92%8CinnerText%E7%9A%84%E5%8C%BA%E5%88%AB"><span class="toc-text">innerHTML和innerText的区别</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#nodeType%E5%B1%9E%E6%80%A7"><span class="toc-text">nodeType属性</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#nodeType%E3%80%81nodeName%E3%80%81nodeValue"><span class="toc-text">nodeType、nodeName、nodeValue</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%96%87%E6%A1%A3%E7%9A%84%E5%8A%A0%E8%BD%BD"><span class="toc-text">文档的加载</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%88%91%E7%9A%84%E5%85%AC%E4%BC%97%E5%8F%B7"><span class="toc-text">我的公众号</span></a></li></ol>
</div>
            </main>
            <aside class="right-column">
              <div class="sticky-widescreen">
  
  
    <article class="card card-content toc-card">
      <div class="toc-header">
  <i 
    class="iconfont icon-menu" 
    style="padding-right: 2px;">
  </i>目录
</div>
<ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%B8%B8%E8%A7%81%E6%A6%82%E5%BF%B5"><span class="toc-text">常见概念</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#JavaScript%E7%9A%84%E7%BB%84%E6%88%90"><span class="toc-text">JavaScript的组成</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%8A%82%E7%82%B9"><span class="toc-text">节点</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BB%80%E4%B9%88%E6%98%AFDOM"><span class="toc-text">什么是DOM</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#DOM%E5%8F%AF%E4%BB%A5%E5%81%9A%E4%BB%80%E4%B9%88"><span class="toc-text">DOM可以做什么</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%85%83%E7%B4%A0%E8%8A%82%E7%82%B9%E7%9A%84%E8%8E%B7%E5%8F%96"><span class="toc-text">元素节点的获取</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#DOM%E8%AE%BF%E9%97%AE%E5%85%B3%E7%B3%BB%E7%9A%84%E8%8E%B7%E5%8F%96"><span class="toc-text">DOM访问关系的获取</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%8E%B7%E5%8F%96%E7%88%B6%E8%8A%82%E7%82%B9"><span class="toc-text">获取父节点</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%8E%B7%E5%8F%96%E5%85%84%E5%BC%9F%E8%8A%82%E7%82%B9"><span class="toc-text">获取兄弟节点</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%8E%B7%E5%8F%96%E5%8D%95%E4%B8%AA%E7%9A%84%E5%AD%90%E8%8A%82%E7%82%B9"><span class="toc-text">获取单个的子节点</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%8E%B7%E5%8F%96%E6%89%80%E6%9C%89%E7%9A%84%E5%AD%90%E8%8A%82%E7%82%B9"><span class="toc-text">获取所有的子节点</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#DOM%E8%8A%82%E7%82%B9%E7%9A%84%E6%93%8D%E4%BD%9C%EF%BC%88%E9%87%8D%E8%A6%81%EF%BC%89"><span class="toc-text">DOM节点的操作（重要）</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%88%9B%E5%BB%BA%E8%8A%82%E7%82%B9"><span class="toc-text">创建节点</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%8F%92%E5%85%A5%E8%8A%82%E7%82%B9"><span class="toc-text">插入节点</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%88%A0%E9%99%A4%E8%8A%82%E7%82%B9"><span class="toc-text">删除节点</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%A4%8D%E5%88%B6%E8%8A%82%E7%82%B9%EF%BC%88%E5%85%8B%E9%9A%86%E8%8A%82%E7%82%B9%EF%BC%89"><span class="toc-text">复制节点（克隆节点）</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%AE%BE%E7%BD%AE%E8%8A%82%E7%82%B9%E7%9A%84%E5%B1%9E%E6%80%A7"><span class="toc-text">设置节点的属性</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1%E3%80%81%E8%8E%B7%E5%8F%96%E8%8A%82%E7%82%B9%E7%9A%84%E5%B1%9E%E6%80%A7%E5%80%BC"><span class="toc-text">1、获取节点的属性值</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2%E3%80%81%E8%AE%BE%E7%BD%AE%E8%8A%82%E7%82%B9%E7%9A%84%E5%B1%9E%E6%80%A7%E5%80%BC"><span class="toc-text">2、设置节点的属性值</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3%E3%80%81%E5%88%A0%E9%99%A4%E8%8A%82%E7%82%B9%E7%9A%84%E5%B1%9E%E6%80%A7"><span class="toc-text">3、删除节点的属性</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%80%BB%E7%BB%93"><span class="toc-text">总结</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#DOM%E5%AF%B9%E8%B1%A1%E7%9A%84%E5%B1%9E%E6%80%A7-%E8%A1%A5%E5%85%85"><span class="toc-text">DOM对象的属性-补充</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#innerHTML%E5%92%8CinnerText%E7%9A%84%E5%8C%BA%E5%88%AB"><span class="toc-text">innerHTML和innerText的区别</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#nodeType%E5%B1%9E%E6%80%A7"><span class="toc-text">nodeType属性</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#nodeType%E3%80%81nodeName%E3%80%81nodeValue"><span class="toc-text">nodeType、nodeName、nodeValue</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%96%87%E6%A1%A3%E7%9A%84%E5%8A%A0%E8%BD%BD"><span class="toc-text">文档的加载</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%88%91%E7%9A%84%E5%85%AC%E4%BC%97%E5%8F%B7"><span class="toc-text">我的公众号</span></a></li></ol>
    </article>
  
  
  <article class="card card-content">
    <div class="recent-posts-card">
  <div class="recent-posts-header">
    <i 
      class="iconfont icon-wenzhang_huaban" 
      style="padding-right: 2px;">
    </i>最近文章
  </div>
  <div class="recent-posts-list">
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2023-03-09</div>
        <a href="/2023/03/09/13-React%E5%9F%BA%E7%A1%80/11-React%20Navive%E5%88%9D%E8%AF%86/"><div class="recent-posts-item-content">11-React Navive初识</div></a>
      </div>
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2023-03-09</div>
        <a href="/2023/03/09/13-React%E5%9F%BA%E7%A1%80/10-AntD%E6%A1%86%E6%9E%B6%E7%9A%84upload%E7%BB%84%E4%BB%B6%E4%B8%8A%E4%BC%A0%E5%9B%BE%E7%89%87%E6%97%B6%E4%BD%BF%E7%94%A8customRequest%E6%96%B9%E6%B3%95%E8%87%AA%E5%AE%9A%E4%B9%89%E4%B8%8A%E4%BC%A0%E8%A1%8C%E4%B8%BA/"><div class="recent-posts-item-content">10-AntD框架的upload组件上传图片时使用customRequest方法自定义上传行为</div></a>
      </div>
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2023-03-09</div>
        <a href="/2023/03/09/13-React%E5%9F%BA%E7%A1%80/09-AntD%E6%A1%86%E6%9E%B6%E7%9A%84upload%E7%BB%84%E4%BB%B6%E4%B8%8A%E4%BC%A0%E5%9B%BE%E7%89%87%E6%97%B6%E9%81%87%E5%88%B0%E7%9A%84%E4%B8%80%E4%BA%9B%E5%9D%91/"><div class="recent-posts-item-content">09-AntD框架的upload组件上传图片时遇到的一些坑</div></a>
      </div>
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2023-03-09</div>
        <a href="/2023/03/09/13-React%E5%9F%BA%E7%A1%80/08-Ant%20Design%E7%9A%84%E5%9F%BA%E6%9C%AC%E4%BD%BF%E7%94%A8/"><div class="recent-posts-item-content">08-Ant Design的基本使用</div></a>
      </div>
    
  </div>
</div>
  </article>
  
  
</div>
            </aside>
          </div>
        </div>
      </div>
    </div>
     
    <footer class="footer">
  <div class="footer-container">
    <div>
      <div class="footer-dsc">
        <span>
          Copyright ©
          
            2023
          
          
        </span>
        &nbsp;
        <a 
          href="/" 
          class="footer-link">
          前端面试
        </a>
      </div>
    </div>

    
      <div class="footer-dsc">
        
          Powered by
          <a 
            href="https://hexo.io/" 
            class="footer-link" 
            target="_blank" 
            rel="nofollow noopener noreferrer">
            &nbsp;Hexo
          </a>
        
        
          <span>&nbsp;|&nbsp;</span>
        
        
          Theme -
          <a 
            href="https://github.com/theme-kaze" 
            class="footer-link" 
            target="_blank"
            rel="nofollow noopener noreferrer">
            &nbsp;Kaze
          </a>
        
      </div>
    
    
    
    
</footer>
 
    
  <a 
    role="button" 
    id="scrollbutton" 
    class="basebutton" 
    aria-label="回到顶部">
    <i class="iconfont icon-arrowleft button-icon"></i>
  </a>

<a 
  role="button" 
  id="menubutton"
  aria-label="menu button"
  class="basebutton">
  <i class="iconfont icon-menu button-icon"></i>
</a>
<a 
  role="button" 
  id="popbutton" 
  class="basebutton" 
  aria-label="控制中心">
  <i class="iconfont icon-expand button-icon"></i>
</a>
<a 
  role="button" 
  id="darkbutton" 
  class="basebutton darkwidget" 
  aria-label="夜色模式">
  <i class="iconfont icon-weather button-icon"></i>
</a>
<a 
  role="button" 
  id="searchbutton" 
  class="basebutton searchwidget" 
  aria-label="搜索">
  <i class="iconfont icon-search button-icon"></i>
</a> 
     
     
     
      <script>
  var addImgLayout = function () {
    var img = document.querySelectorAll('.post-content img')
    var i
    for (i = 0; i < img.length; i++) {
      var wrapper = document.createElement('a')
      wrapper.setAttribute('href', img[i].getAttribute('data-src'))
      wrapper.setAttribute('aria-label', 'illustration')
      wrapper.style.cssText =
        'width: 100%; display: flex; justify-content: center;'
      if (img[i].alt) wrapper.dataset.caption = img[i].alt
      wrapper.dataset.nolink = true
      img[i].before(wrapper)
      wrapper.append(img[i])
      var divWrap = document.createElement('div')
      divWrap.classList.add('gallery')
      wrapper.before(divWrap)
      divWrap.append(wrapper)
    }
    baguetteBox.run('.gallery')
  }
</script>
<script>
  loadScript(
    "/js/lib/lightbox/baguetteBox.min.js",
    addImgLayout
  )
</script>
 
     
     
    <script src="/js/main.js"></script> 
     
    
      <script>
        var addLazyload = function () {
          var observer = lozad('.lozad', {
            load: function (el) {
              el.srcset = el.getAttribute('data-src')
            },
            loaded: function (el) {
              el.classList.add('loaded')
            },
          })
          observer.observe()
        }
      </script>
      <script>
        loadScript('/js/lib/lozad.min.js', addLazyload)
      </script>
    
    <script src="//instant.page/5.1.0" type="module"
      integrity="sha384-by67kQnR+pyfy8yWP4kPO12fHKRLHZPfEsiSXR8u2IKcTdxD805MGUXBzVPnkLHw"></script>
    
    
  </body>
</html>
